package com.lingyun.financialmanagement.service.pojo;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lingyun.financialmanagement.dao.mapper.UsersMapper;
import com.lingyun.financialmanagement.dao.pojo.Users;
import com.lingyun.financialmanagement.service.AdminService;
import com.lingyun.financialmanagement.utils.JWTUtils;
import com.lingyun.financialmanagement.vo.ErrorCode;
import com.lingyun.financialmanagement.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Value("${redis.token.tokenRolePrefix}")
    private String tokenRolePrefix;

    @Override
    public Result addUser(Users user) {
        String account = user.getAccount();
        String password = user.getPassword();
        String role = user.getRole();
        System.out.println(user);


        // 筛选
        if (account == null || account.isEmpty()) {
            return Result.fail(ErrorCode.NULL_ERROR.getCode(), ErrorCode.NULL_ERROR.getMsg());
        }
        if (password == null || password.isEmpty()) {
            return Result.fail(ErrorCode.NULL_ERROR.getCode(), ErrorCode.NULL_ERROR.getMsg());
        }
        if (role == null || role.isEmpty()) {
            return Result.fail(ErrorCode.NULL_ERROR.getCode(), ErrorCode.NULL_ERROR.getMsg());
        }
        if (!(role.equals("用户") || role.equals("管理员") || role.equals("核销员"))){
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }else{
            if (role.equals("用户")){
                user.setRole("user");
            }
            else if(role.equals("管理员")){
                user.setRole("admin");
            }
            else {
                user.setRole("accountant");
            }
        }

        // 加密密码
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5DigestAsHex);

        int insert = 0;
        try {
            insert = usersMapper.insert(user);
        } catch (Exception e) {
            return Result.fail(ErrorCode.INSERT_ERROR.getCode(), ErrorCode.INSERT_ERROR.getMsg());
        }
        if (insert == 0) {
            return Result.fail(ErrorCode.INSERT_ERROR.getCode(), ErrorCode.INSERT_ERROR.getMsg());
        }

        //~~~~~~~~~~~~~~~  可增加操作日志

        // ~~~~~~~~~~~

        return Result.success(true);
    }

    @Transactional
    @Override
    public Result updateUser(Users user) {
        String account = user.getAccount();
        String role = user.getRole();
        String password = user.getPassword();
        if (account == null || account.isEmpty()) {
            return Result.fail(ErrorCode.ACCOUNT_NULL.getCode(), ErrorCode.ACCOUNT_NULL.getMsg());
        }

        UpdateWrapper<Users> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("account", account);

        if (!(role == null || role.isEmpty())) {
            if (role.equals("管理员")){
                updateWrapper.set("role", "admin");
            }
            else if(role.equals("用户")){
                updateWrapper.set("role", "user");
            }
            else if(role.equals("核销员")){
                updateWrapper.set("role", "accountant");
            }
        }

        if (!(password == null || password.isEmpty())) {
            // 密码加密修改
            password = DigestUtils.md5DigestAsHex(password.getBytes());
            updateWrapper.set("password", password);
        }

        try{
            usersMapper.update(updateWrapper);
            if (role != null){
                redisTemplate.opsForValue().set(tokenRolePrefix + account, role);
            }
        } catch (Exception e) {
            throw new RuntimeException("更新失败");
        }

        return Result.success(true);
    }

    @Override
    public Result selectAllUser() {
        List<Users> users = usersMapper.selectList(null);
        return Result.success(users);
    }

}
